/**
 * Copyright (c) 2010 Andrey Somov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yaml.scalayaml.events

import org.yaml.scalayaml.error.Mark

/**
 * Basic unit of output from a {@link org.yaml.snakeyaml.parser.Parser} or input
 * of a {@link org.yaml.snakeyaml.emitter.Emitter}.
 */
abstract class Event (startMark: Mark, endMark: Mark) {
    val id: EventID
}
abstract class NodeEvent (anchor: String, startMark: Mark, endMark: Mark) extends Event (startMark, endMark)
/**
 * Base class for the end events of the collection nodes.
 */
abstract class CollectionEndEvent (startMark: Mark, endMark: Mark) extends Event (startMark, endMark)
/**
 * Base class for the start events of the collection nodes.
 */
// _implicit - flag of a collection start event indicates if the tag may be omitted when the collection is emitted
// flowStyle - flag indicates if a collection is block or flow
abstract class CollectionStartEvent (anchor: String, tag: String, _implicit: Boolean, startMark: Mark, endMark: Mark, flowStyle: Boolean) 
	extends NodeEvent (anchor, startMark, endMark)

sealed class EventID
object Alias extends EventID
object DocumentEnd extends EventID
object DocumentStart extends EventID
object MappingEnd extends EventID
object MappingStart extends EventID
object Scalar extends EventID
object SequenceEnd extends EventID
object SequenceStart extends EventID
object StreamEnd extends EventID
object StreamStart extends EventID

/**
 * Marks the inclusion of a previously anchored node.
 */
case class AliasEvent (anchor: String, startMark: Mark, endMark: Mark) extends NodeEvent (anchor, startMark, endMark) {val id = Alias}

/**
 * Marks the end of a document.
 * <p>
 * This event follows the document's content.
 * </p>
 */
case class DocumentEndEvent (anchor: String, startMark: Mark, endMark: Mark) extends Event (startMark, endMark) {val id = DocumentEnd}

/**
 * Marks the beginning of a document.
 * <p>
 * This event followed by the document's content and a {@link DocumentEndEvent}.
 * </p>
 */
case class DocumentStartEvent (anchor: String, startMark: Mark, endMark: Mark, explicit: Boolean, version: (Int, Int), tags: Map[String, String]) 
	extends Event (startMark, endMark) {val id = DocumentStart}

/**
 * Marks the end of a mapping node.
 * 
 * @see MappingStartEvent
 */
case class MappingEndEvent (startMark: Mark, endMark: Mark) extends CollectionEndEvent (startMark, endMark) {val id = MappingEnd}

/**
 * Marks the beginning of a mapping node.
 * <p>
 * This event is followed by a number of key value pairs. <br>
 * The pairs are not in any particular order. However, the value always directly
 * follows the corresponding key. <br>
 * After the key value pairs follows a {@link MappingEndEvent}.
 * </p>
 * <p>
 * There must be an even number of node events between the start and end event.
 * </p>
 * 
 * @see MappingEndEvent
 */
case class MappingStartEvent (anchor: String, tag: String, _implicit: Boolean, startMark: Mark, endMark: Mark, flowStyle: Boolean) 
	extends CollectionStartEvent (anchor, tag, _implicit, startMark, endMark, flowStyle) {val id = MappingStart}

/**
 * Marks a scalar value.
 */
// style flag of a scalar event indicates the style of the scalar. Possible
// values are None, '', '\'', '"', '|', '>'

// The implicit flag of a scalar event is a pair of boolean values that
// indicate if the tag may be omitted when the scalar is emitted in a plain
// and non-plain style correspondingly.
case class ScalarEvent (anchor: String, tag: String, _implicit: (Boolean, Boolean), value: String, startMark: Mark, endMark: Mark, style: Char) 
	extends NodeEvent (anchor, startMark, endMark) {val id = Scalar}

/**
 * Marks the end of a sequence.
 * 
 * @see SequenceStartEvent
 */
case class SequenceEndEvent (startMark: Mark, endMark: Mark) extends CollectionEndEvent (startMark, endMark) {val id = SequenceEnd}

/**
 * Marks the beginning of a sequence node.
 * <p>
 * This event is followed by the elements contained in the sequence, and a
 * {@link SequenceEndEvent}.
 * </p>
 * 
 * @see SequenceEndEvent
 */
case class SequenceStartEvent (anchor: String, tag: String, _implicit: Boolean, startMark: Mark, endMark: Mark, flowStyle: Boolean) 
	extends CollectionStartEvent (anchor, tag, _implicit, startMark, endMark, flowStyle) { val id = SequenceStart }

/**
 * Marks the end of a stream that might have contained multiple documents.
 * <p>
 * This event is the last event that a parser emits. Together with
 * {@link StreamStartEvent} (which is the first event a parser emits) they mark
 * the beginning and the end of a stream of documents.
 * </p>
 * <p>
 * See {@link Event} for an exemplary output.
 * </p>
 */
case class StreamEndEvent (anchor: String, startMark: Mark, endMark: Mark) extends Event (startMark, endMark) {val id = StreamEnd }

/**
 * Marks the start of a stream that might contain multiple documents.
 * <p>
 * This event is the first event that a parser emits. Together with
 * {@link StreamEndEvent} (which is the last event a parser emits) they mark the
 * beginning and the end of a stream of documents.
 * </p>
 * <p>
 * See {@link Event} for an exemplary output.
 * </p>
 */
case class StreamStartEvent (anchor: String, startMark: Mark, endMark: Mark) extends Event (startMark, endMark) {val id = StreamStart }